package cn.wolfcode.trip.app.web.controller;

import cn.wolfcode.trip.app.util.UserContext;
import cn.wolfcode.trip.base.domain.*;
import cn.wolfcode.trip.base.query.StrategyQueryObject;
import cn.wolfcode.trip.base.service.*;
import cn.wolfcode.trip.base.util.JSONResult;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/strategyComments")
public class StrategyCommentController {
    @Autowired
    private IStrategyCommentService commentService;
    @Autowired
    private IUserService userService;

    @GetMapping("/{id}")
    @ResponseBody
    public Object getCommentById(@PathVariable Long id){
        StrategyComment comment = commentService.selectByPrimaryKey(id);

        return comment;
    }


    //获取某个评论下的所有子评论
    @GetMapping("/{id}/subComment")
    @ResponseBody
    public Object getSubCommentById(@PathVariable Long id){
        List<SubComment> subComments = commentService.getSubCommentById(id);
        return subComments;
    }

    //保存评论下的评论
    @PostMapping("saveSubComment")
    @ResponseBody
    public Object insertCon(SubComment subComment){
        JSONResult json = new JSONResult();
        System.out.println(subComment);
        try{
        commentService.insertSubComment(subComment);

        }catch (Exception e){
            e.printStackTrace();
            json.mark(e.getMessage());
        }
        return json;
    }


    //查询我的评论下的评论数据
    @GetMapping("/getsubContent/{id}")
    @ResponseBody
    public Object getSubContentByUserId(@PathVariable Long id){
        List<SubComment> subComments=new ArrayList<>();
        try{
            subComments= commentService.getSubContentByUserId(id);


        }catch (Exception e){
            e.printStackTrace();
        }
        return subComments;
    }

    //查询我的评论数据
    @GetMapping("/getMyComments/{id}")
    @ResponseBody
    public Object getMyComments(@PathVariable Long id){
        List<StrategyComment> comment=new ArrayList<>();
        try{
            comment= commentService.selectByAuthorId(id);


        }catch (Exception e){
            e.printStackTrace();
        }
        return comment;
    }
    /**
     * 获得未读子评论的总数
     * @return
     */
    @GetMapping("/getCountHasReadSub/{id}")
    @ResponseBody
    public Object getCountHasReadComment(@PathVariable Long id){
        int subComments=0;
        try{
            subComments= commentService.getCountHasReadComment(id);


        }catch (Exception e){
            e.printStackTrace();
        }
        return subComments;

    }

    /**
     * 设置子评论为已读
     * @return
     */
    @GetMapping("/setHasRead/{id}")
    @ResponseBody
    public Object setHasRead(@PathVariable Long id){
        JSONResult jsonResult = new JSONResult();
        Map<String,Object> map =new HashMap<>();
        map.put("id",id);
        map.put("state",1);

        try{
            commentService.setSubHasRead(map);

        }catch (Exception e){
            e.printStackTrace();
            jsonResult.mark(e.getMessage());
        }
        return jsonResult;

    }



    //查询攻略中的标签(大家都在看)
    @GetMapping("/{id}/tag")
    @ResponseBody
    public Object getCommentByStrategyId(@PathVariable Long id){
        List<StrategyComment> comments = commentService.getCommentByStrategyId(id);
        return comments;
    }

    //点评头条(根据状态去查)
    @GetMapping("/state")
    @ResponseBody
    public Object selectCommentByStarNum(){
        List<StrategyComment> comments = commentService.selectCommentByState();
        return comments;
    }

    //查询@我的评论
    @GetMapping("/mentions")
    @ResponseBody
    public Object getMentions(){
        //传过来的用户id,获取用户信息
        User user = UserContext.getCurrentUser();
        String nickName = user.getNickName();
        String keyword="@"+nickName;
        System.out.println(keyword);
        //以@用户名为关键字,查询
        List<StrategyComment> strategyComments = commentService.selectByMentions(keyword);
        //更新状态为已读
        commentService.setCommentsHasRead(keyword);
        return strategyComments;
    }
    //查询未读@我的评论
    @GetMapping("/noReadMentions")
    @ResponseBody
    public Object getNoReadMentions(){
        //传过来的用户id,获取用户信息
        User user = UserContext.getCurrentUser();

        String nickName = user.getNickName();

        String keyword="@"+nickName;
        //以@用户名为关键字,查询
        Integer strategyComments = commentService.getNoReadMentions(keyword,0);
        return strategyComments;
    }

    //更新@我的状态为已读




    //添加评论
    @PostMapping("/addComment")
    @ResponseBody
    public Object addComment(StrategyComment comment,String tags){
        JSONResult result = new JSONResult();
        try {
            comment.setUser(UserContext.getCurrentUser());
            commentService.addComment(comment,tags);
        }catch (Exception e){
            e.printStackTrace();
            result.mark("保存失败");
        }

        return result;
    }


    @GetMapping("/count")
    @ResponseBody
    public Object getCount(Long id){
        return commentService.getCount(id);
    }
}
